<?php
class ServiceController extends AdminAppController
{
    public $uses = array('Admin.Service');
    public $paginate = array(
        'limit' => 25,
        'order' => array(
            'Service.priority' => 'desc'
        ),
        'conditions' => array(
            'Service.is_deleted' => 0
        )
    );

    public function index()
    {
        $this->Service->contain();
        $data = $this->paginate('Service');
        $this->set(compact('data'));
    }

    public function add()
    {
        if ($this->request->is('post')) {
            try {
                $this->Service->saveService($this->request->data['Service']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Service has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Service->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function edit($id=null)
    {
        $this->Service->contain();
        $service = $this->Service->findById($id);
        if ($this->request->is('get')) {
            $this->request->data = $service;
        } else {
            try {
                $isUpdate = true;
                $this->Service->saveService($this->request->data['Service'], $isUpdate, $service['Service']);
                $this->redirect(array('action' => 'index'));
                $this->Session->setFlash(__('The Service has been saved'));
            } catch (Exception $e) {
                $validationErrors = $this->Service->validationErrors;
                $this->set(compact('validationErrors'));
                $this->Session->setFlash($e->getMessage());
            }
        }
    }

    public function delete($id=null)
    {
        if ($id == null) {
            $this->redirect('/');
        }
        $data = array(
            'Service' => array(
                'id' => $id,
                'is_deleted' => 1
            )
        );
        $this->Service->save($data);
        $this->redirect('/admin/service');
    }

}
